//
//  LZPlayViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "LZPlayViewController.h"
#import "FitHeader.h"
#import "UIImage+GaussianBlur.h"
#import "LZListOrHistroyViewController.h"
#import "LZPlayOrPauseTableViewCell.h"
#import "TwoToOne.h"
#import "LZBillboardModel.h"
#import "UIImageView+WebCache.h"
#import "LZMusicSingleton.h"
#import "TabBarController.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
#import "TabBarController.h"
#import "Model.h"
#import "UIImage+GIF.h"

@interface LZPlayViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, strong)NSTimer *timer;
@property(nonatomic, strong)AVPlayer *player;
@property(nonatomic, strong)UIProgressView *progressView;//播放进度条
@property(nonatomic, strong)BaseLabel *LZlabel;
@property(nonatomic, strong)id progressViewObserver;//播放进度条观察者
@property(nonatomic, strong)NSDictionary *historyDic;
@property(nonatomic, strong)UISlider *slider;
@property(nonatomic, strong)UIView *backView;
@property(nonatomic, assign)CGFloat totalTime;
@property(nonatomic, assign)CGFloat currentTime;
@property(nonatomic, strong)NSMutableArray *zbbDataArray;
@property(nonatomic, strong)UIView *zbbView;
@property(nonatomic, strong)UIImageView *zbbBackImageView;
@property(nonatomic, strong)UIImageView *zbbImageView;
@property(nonatomic, strong)UILabel *zbbTitleLabel;
@property(nonatomic, strong)MBProgressHUD *progressViewHUB;
@property(nonatomic, assign)NSInteger page;


@end

@implementation LZPlayViewController
#pragma mark - 视图将要出现时
- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.tintColor = [UIColor clearColor];
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [Model shareInstance].ViewColor;
    _tableView.backgroundColor = [Model shareInstance].ViewColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"cell0"] == 1) {
        
        [self getData];
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"play"];
        _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
        
        [TabBarController shareInstance].playButton.alpha = 0;
        [[LZMusicSingleton shareInstance] play:_dicArray[_index]];
        self.dic = _dicArray[_index];
        _totalTime = [[_dicArray[_index] objectForKey:@"duration"]floatValue];
        _currentTime = 0;
        [[LZMusicSingleton shareInstance] play:_dic];
    }
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(transformAction) userInfo:nil repeats:YES];
    });
    
//    self.progressViewHUB = [[MBProgressHUD alloc]initWithView:self.view];
//    _progressViewHUB.labelText = @"努力加载中...";
//    [_progressViewHUB setMode:0];
//    [_progressViewHUB show:YES];
//    [self.view addSubview:_progressViewHUB];
}
//- (void)viewDidDisappear:(BOOL)animated{
//    [_progressViewHUB removeFromSuperview];
//}
- (void)viewDidAppear:(BOOL)animated{
    [_progressViewHUB removeFromSuperview];
}
-(void)creatView{
    _flag = 0;
    _dataSourceArray = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:_tableView];
    
    _backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 540 * FITHeight)];
    _backImageView.userInteractionEnabled = YES;
    _backImageView.backgroundColor = [UIColor colorWithRed:0.8 green:0.84 blue:0.88 alpha:1];
    [_backImageView sd_setImageWithURL:[NSURL URLWithString:[_ZBBdic objectForKey:@"picPath"]]];
    
    [self.view addSubview:_backImageView];
    
    _button = [BaseButton buttonWithType:UIButtonTypeSystem];
    _button.frame = CGRectMake(20 * FITWidth, 30 * FITHeight, 30 * FITWidth, 30 * FITHeight);
    [_button setTintColor:[UIColor whiteColor]];
    [_button setImage:[UIImage imageNamed:@"下箭头"] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(dismissAction) forControlEvents:UIControlEventTouchUpInside];
    [_backImageView addSubview:_button];
    
    _titleLabel = [[BaseLabel alloc]initWithFrame:CGRectMake(_button.frame.origin.x + _button.frame.size.width + 10 * FITWidth, _button.frame.origin.y, 250 * FITWidth, _button.frame.size.height)];
    _titleLabel.font = [UIFont systemFontOfSize:17];
    _titleLabel.textColor = [UIColor redColor];
    [_backImageView addSubview:_titleLabel];
    
    _playtimesLabel = [[BaseLabel alloc]initWithFrame:CGRectMake(_titleLabel.frame.origin.x + _titleLabel.frame.size.width, _titleLabel.frame.origin.y, 50 * FITWidth, _titleLabel.frame.size.height)];
    _playtimesLabel.font = [UIFont systemFontOfSize:14];
    _playtimesLabel.textColor = [UIColor whiteColor];
    [_backImageView addSubview:_playtimesLabel];
    
    _coverMiddleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_titleLabel.frame.origin.x + 20 * FITWidth, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 80 * FITHeight, 215 * FITWidth, 215 * FITHeight)];
    [_coverMiddleImageView sd_setImageWithURL:[NSURL URLWithString:_lzBill.coverMiddle] placeholderImage:[UIImage imageNamed:@"200*200"]];
    _coverMiddleImageView.layer.cornerRadius = (215 / 2) * FITWidth;
    _coverMiddleImageView.clipsToBounds = YES;
    [_backImageView addSubview:_coverMiddleImageView];
    
//    _downloadButton = [BaseButton buttonWithType:UIButtonTypeCustom];
//    _downloadButton.frame = CGRectMake(_coverMiddleImageView.frame.origin.x + _coverMiddleImageView.frame.size.width + 20 * FITWidth, _coverMiddleImageView.frame.origin.y + 50 * FITHeight, 32 * FITWidth, 32 * FITHeight);
//    [_downloadButton setImage:[UIImage imageNamed:@"下载白色"] forState:UIControlStateNormal];
//    [_downloadButton addTarget:self action:@selector(downloadAction) forControlEvents:UIControlEventTouchUpInside];
    
//    _shareButton = [BaseButton buttonWithType:UIButtonTypeCustom];
//    _shareButton.frame = CGRectMake(_downloadButton.frame.origin.x, _downloadButton.frame.origin.y + _downloadButton.frame.size.height + 40 * FITHeight, _downloadButton.frame.size.width, _downloadButton.frame.size.height);
//    [_shareButton addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
//    [_shareButton setImage:[UIImage imageNamed:@"分享白色"] forState:UIControlStateNormal];
//    [_backImageView addSubview:_shareButton];
    
//    [_backImageView addSubview:_downloadButton];
    
    _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
    _progressView.frame = CGRectMake(_coverMiddleImageView.frame.origin.x - 40 * FITWidth, _coverMiddleImageView.frame.origin.y + _coverMiddleImageView.frame.size.height + 50 * FITHeight, _coverMiddleImageView.frame.size.width + 40 * FITWidth, 20);
    [_backImageView addSubview:_progressView];
    
    _LZlabel = [[BaseLabel alloc] initWithFrame:CGRectMake(_progressView.frame.origin.x + _progressView.frame.size.width + 3 * FITWidth, _progressView.frame.origin.y - 10 * FITHeight, 60 * FITWidth, 20 * FITHeight)];
    [_LZlabel setFontSize:10];
    _LZlabel.textColor = [UIColor lightGrayColor];
    [_backImageView addSubview:_LZlabel];
    
    _listImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_button.frame.origin.x, _coverMiddleImageView.frame.origin.y + _coverMiddleImageView.frame.size.height + 110 * FITHeight, 32 * FITWidth, 32 * FITHeight)];
    _listImageView.image = [UIImage imageNamed:@"播放列表"];
    _listImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *listTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(listAction)];
    [_listImageView addGestureRecognizer:listTap];
    [_backImageView addSubview:_listImageView];
    
    _goBackImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_coverMiddleImageView.frame.origin.x + 20 * FITWidth, _listImageView.frame.origin.y, _listImageView.frame.size.width, _listImageView.frame.size.height)];
    _goBackImageView.image = [UIImage imageNamed:@"上一首"];
    _goBackImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *goBackTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goBackAction)];
    [_goBackImageView addGestureRecognizer:goBackTap];
    
    [_backImageView addSubview:_goBackImageView];
    
    _playOrPauseImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_coverMiddleImageView.frame.origin.x + 85 * FITWidth, _goBackImageView.frame.origin.y - 10 * FITHeight, 48 * FITWidth, 48 * FITHeight)];
    _playOrPauseImageView.image = [UIImage imageNamed:@"播放白色"];
    _playOrPauseImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(playOrPauseAction)];
    [_playOrPauseImageView addGestureRecognizer:tap];
    [_backImageView addSubview:_playOrPauseImageView];
    
    _goNextImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_coverMiddleImageView.frame.origin.x + _coverMiddleImageView.frame.size.width - 52 * FITWidth, _goBackImageView.frame.origin.y, _goBackImageView.frame.size.width, _goBackImageView.frame.size.height)];
    _goNextImageView.image = [UIImage imageNamed:@"下一首"];
    _goNextImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *goNextTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goNextAction)];
    [_goNextImageView addGestureRecognizer:goNextTap];
    [_backImageView addSubview:_goNextImageView];
    
    _historyImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_playtimesLabel.frame.origin.x, _goBackImageView.frame.origin.y, _listImageView.frame.size.width, _listImageView.frame.size.height)];
    _historyImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *historyTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(historyAction)];
    [_historyImageView addGestureRecognizer:historyTap];
    _historyImageView.image = [UIImage imageNamed:@"播放历史"];
    [_backImageView addSubview:_historyImageView];
    
    _tableView.tableHeaderView = _backImageView;
    
    self.progressViewHUB = [[MBProgressHUD alloc]initWithView:self.view];
    [[[_progressViewHUB subviews]firstObject] removeFromSuperview];
    [[[_progressViewHUB subviews] objectAtIndex:1] removeFromSuperview];
    [_progressViewHUB setMode:0];
    [_progressViewHUB show:YES];
    NSString  *name = @"RED.gif";
    _progressViewHUB.color = [UIColor clearColor];
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(-70 * FITWidth, -80 * FITHeight, 140 * FITWidth, 100 * FITHeight)];
    imageView.layer.masksToBounds = YES;
    imageView.layer.cornerRadius = 10;
    imageView.opaque = NO;
    
    [[[[[_progressViewHUB subviews] lastObject] subviews] firstObject] removeFromSuperview];
    
    imageView.image = [UIImage sd_animatedGIFWithData:imageData];
    [[[_progressViewHUB subviews] lastObject] addSubview:imageView];
    [self.view addSubview:_progressViewHUB];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    

    //创建一个计时器使之自动循环播放
    //    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    //    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    //    [runLoop addTimer:_timer forMode:NSRunLoopCommonModes];
    
    
    //创建一个计时器使之自动循环播放
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:_timer forMode:NSRunLoopCommonModes];
    
    [self getData];
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"play"];
    
    _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
    
    [[LZMusicSingleton shareInstance] play:_dic];
    
    _totalTime = [[_dic objectForKey:@"duration"]floatValue];
    _currentTime = 0;
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeProgressView) userInfo:nil repeats:YES];
    
    /*
     __block AVPlayer *currentPlayer = [LZMusicSingleton shareInstance].player;
     [[LZMusicSingleton shareInstance].player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
     float currentTime = CMTimeGetSeconds(currentPlayer.currentTime) / 60.0;
     float totalTime = CMTimeGetSeconds(currentPlayer.currentItem.duration) / 60.0;
     NSLog(@"%.5f, %.5f", currentTime, totalTime);
     [_progressView setProgress:currentTime / totalTime animated:YES];
     }];
     */
    [self transformAction];
}

#pragma mark - 改变progressView进度条
- (void)changeProgressView
{
    if ([LZMusicSingleton shareInstance].player.rate == 1) {
        if (![[NSUserDefaults standardUserDefaults] integerForKey:@"play"]){
            
            CGFloat progress = _currentTime / _totalTime;
            _currentTime++;
            if (_currentTime >= _totalTime) {
                _index++;
                
                [self getData];
            }
            NSInteger total_min = _totalTime / 60;
            NSInteger total_sec = _totalTime - total_min * 60;
            NSInteger current_min = _currentTime / 60;
            NSInteger current_sec = _currentTime - current_min * 60;
            NSString *total_minString = @"";
            NSString *total_secString = @"";
            NSString *current_minString = @"";
            NSString *current_secString = @"";
            if (total_min < 10) {
                total_minString = [NSString stringWithFormat:@"0%ld", total_min];
            }else{
                total_minString = [NSString stringWithFormat:@"%ld", total_min];
            }
            if (total_sec < 10) {
                total_secString = [NSString stringWithFormat:@"0%ld", total_sec];
            }else{
                total_secString = [NSString stringWithFormat:@"%ld", total_sec];
            }
            if (current_min < 10) {
                current_minString = [NSString stringWithFormat:@"0%ld", current_min];
            }else{
                current_minString = [NSString stringWithFormat:@"%ld", current_min];
            }
            if (current_sec < 10) {
                current_secString = [NSString stringWithFormat:@"0%ld", current_sec];
            }else{
                current_secString = [NSString stringWithFormat:@"%ld", current_sec];
            }
            //        NSLog(@"%ld %ld %ld %ld", current_min, current_sec, total_min, total_sec);
            _LZlabel.text = [NSString stringWithFormat:@"%@.%@/%@.%@", current_minString, current_secString, total_minString, total_secString];
            [_progressView setProgress:progress animated:YES];
        }

    }else{
        _playOrPauseImageView.image = [UIImage imageNamed:@"播放白色"];
    }
}


#pragma mark - 图片旋转
- (void)transformAction{
    //    //旋转
    //    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    //    basicAnimation.fromValue = [NSNumber numberWithInteger:0];
    //    basicAnimation.toValue = [NSNumber numberWithInteger:M_PI * 100];
    //    [basicAnimation setDuration:500];
    //    [basicAnimation setRepeatCount:NSIntegerMax];
    //    [_coverMiddleImageView.layer addAnimation:basicAnimation forKey:@""];
    
    //明哥 吊~
    if ([LZMusicSingleton shareInstance].player.rate == 1) {
        if (![[NSUserDefaults standardUserDefaults] integerForKey:@"play"]) {
            _coverMiddleImageView.transform = CGAffineTransformRotate(_coverMiddleImageView.transform, 2 * M_PI / 60.0 / 10);
        }
    }
}
#pragma mark - 上一曲
- (void)goBackAction{
    _index--;
    if (_index == 0) {
        _goBackImageView.image = [UIImage imageNamed:@"上一首灰色"];
        _goBackImageView.userInteractionEnabled = NO;
        [[LZMusicSingleton shareInstance] play:_dicArray[_index]];
        _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
        _totalTime = [[_dic objectForKey:@"duration"]floatValue];
        _currentTime = 0;
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"play"];
        return;
    }
    
    if (_index > 0) {
        _goBackImageView.userInteractionEnabled = YES;
        _goBackImageView.image = [UIImage imageNamed:@"上一首"];
        _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
        _totalTime = [[_dic objectForKey:@"duration"]floatValue];
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"play"];
        [[LZMusicSingleton shareInstance] play:_dicArray[_index]];
        _currentTime = 0;


        

        [self getData];
        
    }
}
#pragma mark - 下一曲
- (void)goNextAction{
    _index++;
    if (_index == _dicArray.count) {
        _goNextImageView.image = [UIImage imageNamed:@"下一首白色"];
        _goNextImageView.userInteractionEnabled = NO;
        _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
        _currentTime = 0;
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"play"];
        [self getData];
        _totalTime = [[_dic objectForKey:@"duration"]floatValue];
        
        
        return;
    }
    if (_index < _dicArray.count) {
        [[LZMusicSingleton shareInstance] play:_dicArray[_index]];
        _goNextImageView.userInteractionEnabled = YES;
        _goNextImageView.image = [UIImage imageNamed:@"下一首"];
        _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
        
        _currentTime = 0;
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"play"];
        
        [self getData];
        _totalTime = [[_dic objectForKey:@"duration"]floatValue];
        
        
    }
    
}
#pragma mark - 播放或者暂停
- (void)playOrPauseAction{
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"play"] == 0) {
        _playOrPauseImageView.image = [UIImage imageNamed:@"播放白色"];
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"play"];
        
        
        [TabBarController shareInstance].playButton.alpha = 0.5;
        //[_coverMiddleImageView.layer removeAllAnimations];
        
        [[LZMusicSingleton shareInstance].player pause];
    }else{
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"play"];
        _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
        [TabBarController shareInstance].playButton.alpha = 0;
        [[LZMusicSingleton shareInstance].player play];
               [[LZMusicSingleton shareInstance] play:_dic];
        //[self transformAction];
    }
}
- (void)timerAction
{
    CGRect frame = CGRectMake(_button.frame.origin.x + _button.frame.size.width + 10 * FITWidth, _button.frame.origin.y, 550 * FITWidth, _button.frame.size.height);
    frame.origin.x = 320 * FITWidth;
    self.titleLabel.frame = frame;
    [UIView beginAnimations:@"testAnimation"context:NULL];
    [UIView setAnimationDuration:5];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatAutoreverses:NO];
    frame = self.titleLabel.frame;
    frame.origin.x = - _titleLabel.frame.size.width;
    self.titleLabel.frame = frame;
    [UIView commitAnimations];
    
}

- (void)getData{
    _dataSourceArray = [NSMutableArray array];
    [_tableView reloadData];
//    [_progressViewHUB removeFromSuperview];
    [_progressViewHUB removeFromSuperview];
    
    if ([_dataArray[_index] trackId] == nil) {
        _currentTime = 0;
        _urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/track/detail?device=android&trackId=%@", [_dataArray[_index] ID]];
        [TwoToOne getDataByString:_urlString BodyString:nil WithDataBlock:^(id data) {
            _dataSourceArray = [NSMutableArray array];
            _historyDic = data;
            _totalTime = [[_dicArray[_index] objectForKey:@"duration"]floatValue];
  
            [_dataSourceArray addObject:_historyDic];
            [[LZMusicSingleton shareInstance] play:_dicArray[_index]];
            [_tableView reloadData];
            [_progressViewHUB removeFromSuperview];
            
        }];
    }else{

        

        _currentTime = 0;

        _urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/track/detail?device=android&trackId=%@", [_dataArray[_index] trackId]];
        [TwoToOne getDataByString:_urlString BodyString:nil WithDataBlock:^(id data) {
            _dataSourceArray = [NSMutableArray array];
            LZBillboardModel *lzBil = [[LZBillboardModel alloc]init];
            [lzBil setValuesForKeysWithDictionary:data];
            [_dataSourceArray addObject:lzBil];
            _historyDic = data;
            _totalTime = [[_dicArray[_index] objectForKey:@"duration"]floatValue];
            [[LZMusicSingleton shareInstance] play:_dicArray[_index]];
            [_tableView reloadData];
            [_progressViewHUB removeFromSuperview];
        }];

    }
}
//#pragma mark - 下载
//- (void)downloadAction{
//    NSLog(@"下载");
//    if (_flag == 0) {
//        [_downloadButton setImage:[UIImage imageNamed:@"对号白色"] forState:UIControlStateNormal];
//    }
//}
//#pragma mark - 分享
//- (void)shareAction{
//    NSLog(@"分享");
//}
#pragma mark - cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90 * FITHeight;
}
#pragma mark - cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    _backImageView.image = [UIImage imageNamed:@""];
    _coverMiddleImageView.image = [UIImage imageNamed:@""];
    return _dataSourceArray.count;
}
//#pragma mark - 点击跳转
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//



#pragma mark - cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"cell";
    LZPlayOrPauseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[LZPlayOrPauseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = 0;
    cell.backgroundColor = [UIColor whiteColor];
    
    if ([_dataArray[_index] trackId] == nil) {
        _titleLabel.text = [_historyDic objectForKey:@"title"];
        [_coverMiddleImageView sd_setImageWithURL:[NSURL URLWithString:[_historyDic objectForKey:@"coverMiddle"]] placeholderImage:[UIImage imageNamed:@"转盘"]];
        _backImageView.image = [UIImage imageWithBlurImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[_historyDic objectForKey:@"coverLarge"]]]] intputRadius:20];
        _playtimesLabel.text = [NSString stringWithFormat:@"%.1f万", [[_historyDic objectForKey:@"playtimes"] integerValue] / 10000.0];
        cell.dic = _historyDic;
    }else{
        
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_group_t group = dispatch_group_create();
        LZBillboardModel *lzBill = _dataSourceArray[indexPath.row];
        dispatch_group_async(group, queue, ^{
            _titleLabel.text = lzBill.title;
            [_coverMiddleImageView sd_setImageWithURL:[NSURL URLWithString:lzBill.coverMiddle] placeholderImage:[UIImage imageNamed:@"转盘"]];
            _playtimesLabel.text = [NSString stringWithFormat:@"%.1f万", [lzBill.playtimes integerValue] / 10000.0];
        });
        dispatch_group_async(group, queue, ^{
            
            _backImageView.image = [UIImage imageWithBlurImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:lzBill.coverLarge]]] intputRadius:20];
        });
//      dispatch_group_async(group, queue, ^{
//          [[LZMusicSingleton shareInstance] play:_dicArray[_index]];
//      });
        cell.lzBill = lzBill;
    }
    
    if (_index == 0) {
        _goBackImageView.image = [UIImage imageNamed:@"上一首灰色"];
        _goBackImageView.userInteractionEnabled = NO;
    }else{
        _goBackImageView.image = [UIImage imageNamed:@"上一首"];
        _goBackImageView.userInteractionEnabled = YES;
    }if (_index == _dicArray.count - 1) {
        _goNextImageView.image = [UIImage imageNamed:@"下一首灰色"];
        _goNextImageView.userInteractionEnabled = NO;
    }else{
        _goNextImageView.image = [UIImage imageNamed:@"下一首"];
        _goNextImageView.userInteractionEnabled = YES;
    }
    
    return cell;
}
#pragma mark - 播放列表
-(void)listAction
{
    if (_dicArray.count == 1) {
        return;
    }
    _flag = 100;
    LZListOrHistroyViewController *lzList = [[LZListOrHistroyViewController alloc]init];
    lzList.titleString = @"播放列表";
    lzList.index = _flag;
    
    lzList.dataArray = _dataArray;
    
    void(^myBlock)(NSInteger indexPath) = ^(NSInteger indexPath){
        _index = indexPath;
        
        
        [self getData];
        
        
    };
    lzList.block = myBlock;
    lzList.dic = _historyDic;
    
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"cell0"];
    [self.navigationController pushViewController:lzList animated:YES];
    
    
}
#pragma mark - 播放历史
- (void)historyAction{
    _flag = 200;
    LZListOrHistroyViewController *lzListOrHistroy = [[LZListOrHistroyViewController alloc]init];
    lzListOrHistroy.titleString = @"播放历史";
    lzListOrHistroy.index = _flag;
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"cell0"];
    [self.navigationController pushViewController:lzListOrHistroy animated:YES];
}
- (void)dismissAction{
    
    [[TabBarController shareInstance] AudioWillPlay:[[NSUserDefaults standardUserDefaults] integerForKey:@"play"]];
    
    
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"cell0"];
    [UIView animateWithDuration:2 animations:^{
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}
+ (LZPlayViewController *)shareInstance{
    static LZPlayViewController *lzPlay = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lzPlay = [[LZPlayViewController alloc]init];
        //[NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(transformAction) userInfo:nil repeats:YES];
    });
    return lzPlay;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
